package observerexample.bugaoban;



import observerexample.dataobj.NewWeatherData;
import observerexample.supperobj.DisplayElement;

import java.util.Observable;
import java.util.Observer;


/**
 * @author yaojianfeng
 * 布告板
 */
public class NewCurrentConditionsDisplay implements Observer, DisplayElement {
    private float temperature;
    private float humidity;
    private float pressure;
    private Observable observable;
    private int i = 1;

    /**
     * 构造器 需要一个 Observable 对象 (也就是主题) 作为注册之用
     * @param observable 气候数据源
     */
    public NewCurrentConditionsDisplay(Observable observable) {
        this.observable = observable;
        observable.addObserver(this);
    }

    @Override
    public void display() {
        System.out.println("Current condition:" + temperature + "F degrees and" + humidity + "% humidity" + pressure+"Pressure");
    }


    /**
     * This method is called whenever the observed object is changed. An
     * application calls an <tt>Observable</tt> object's
     * <code>notifyObservers</code> method to have all the object's
     * observers notified of the change.
     *
     * @param observable   the observable object.
     * @param arg an argument passed to the <code>notifyObservers</code>
     */
    @Override
    public void update(Observable observable, Object arg) {
        if (observable instanceof NewWeatherData){
            NewWeatherData newWeatherData = (NewWeatherData) observable;
            this.temperature = newWeatherData.getTemp();
            this.humidity = newWeatherData.getHumidity();
            this.pressure = newWeatherData.getPressure();
            display();

            System.out.println("第"+i+"次更新三种数据的布告板");
            i++;
        }
    }
}
